/* General functions for calculating and searching fields etc. */

// Search an item from an array
// PARAMS:
//  item       The item to search for
//  searchArr  The array to search from
// RETURN:
//  The index of the element if found. Otherwise -1
function findArray(item, searchArr) {
    var result = -1;

    acount = searchArr.length;
    c = 0;
    while ((result == -1) && (acount < c))
      if (item == searchArr[c++]) result = c;

    return result;
} // findGroupEnd


// Finds the First given field name starting in the given group from
// the given row
// PARAMS:
//  fieldName   The name of the field search for
//  group       The group to search for. If -1 -> not used
//  Row         The number of the row where to search in the group.
//              If -1 -> not used
//
//  Note!! if group is not given but row is then the row means the row index in
//  the CompMap table!
//  RETURNS:
//      The whole name of the found component if not found en empty string
function findField(fieldName, group, row) {
    /* If group is given search for the start of the group */
    if (group >= 0)
        startRow = findGroupStart(group)
    else
        startRow = 0;

    if (row > 0)
        startRow += row;

    while (CompMap[startRow][0] != '') {
        col_no = 0;
        while (CompMap[startRow][col_no] != '') {
            if (extractTag(CompMap[startRow][col_no]) != fieldName) {
                col_no = col_no + 1;
            } else {
                return CompMap[startRow][col_no];
            }
        }
        startRow++;
    } // while (CompMap[startRow][0])

    return '';
} // findField


// Search for the begining of the group in the roaming map
// PARAMS:
//  group       The group to search for
// RETURN:
//  The index of the first row or -1 in case of error
function findGroupStart(group) {
    c = 0;
    // Yes! following if clause cannot be merged with the while
    while (CompMap[c][0])
      if (extractGrp(CompMap[c][0]) == group) break;
      else c = c + 1;

    if (!CompMap[c][0]) return -1;
    else return c;
} // findGroupStart

// Search for the end of the group in the roaming map
// PARAMS:
//  group       The group to search for
// RETURN:
//  The index of the last row or -1 in case of error
function findGroupEnd(group) {
    c = findGroupStart(group);
    if (c < 0)
        return c;

    // Yes! following if clause cannot be merged with the while
    while (CompMap[c][0])
      if (extractGrp(CompMap[c][0]) == group) c = c + 1;
      else break;

    return c - 1;
} // findGroupEnd


/*
 * Calculate result using the formula given.
 * PARAMS:
 *  fieldName   The name of the field to be updated. This is needed to
 *              get the group and row of the colums part of the given formula.
 *  formula     formula to calculate. References to colums are in form $col$.
 * RETURNS:
 *  The result of the calculation
 */
function dzCalculate(fieldName, formula) {
    /* Replace the column tags in the formula */
    result_formula = dzReplaceTags(fieldName, formula);
    result = eval(result_formula);

    return result;
} // dzCalculate


/* Replaces the tags in the formula. Tag are in form $tag$. A tag stands for a
 * column in Dataxi table.
 *
 * PARAMS:
 *  fieldName   The name of the field to be updated. This is needed to
 *              get the group and row of the colums part of the given formula.
 *  formula     formula where to replace the tags.
 *
 * RETURNS:
 *  The replaced formula string.
 */
function dzReplaceTags(fieldName, formula) {
    row = extractRow(fieldName);
    grp = extractGrp(fieldName);

    fArray = formula.split('$');

    /* Replace tags in the formula */
    replaced = '';
    icount = 0;
    while (icount < fArray.length-1) {
        replaced = replaced + fArray[icount];
        tag = fArray[icount+1];
        realName = "number(" +findField(tag, -1, -1) +")";
        if (extractGrp(realName) == grp) {
            /* Same group. First seek from the same row */
            tmp = findField(tag, -1, row);
            /* If not found seek from the entire group */
            if (tmp == "")
                tmp = findField(tag, grp, -1);
            replaced = replaced + 'document.NalleForm.' + tmp + '.value';
        } else {
            // different group
            replaced = replaced + 'document.NalleForm.' + realName + '.value';
        }
        icount = icount + 2;
    }
    /* insert still the tail */
    replaced = replaced + fArray[icount];

    return replaced;
} // dzReplaceTags
